package com.tarena.luban.test.controller;

import com.tarena.luban.test.assemble.WorkerAssemble;
import com.tarena.luban.test.bo.WorkerBO;
import com.tarena.luban.test.service.WorkerService;
import com.tarena.luban.test.vo.WorkerVO;
import com.tarena.passport.protocol.LoginUser;
import com.tarena.passport.sdk.context.SecurityContext;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class WorkerController {
    @Autowired
    private WorkerService workerService;
    /**
     * 前后端分离开发,统一返回数据 Result
     */
    @PostMapping("/worker/create")
    public Result<Long> createWorker(@RequestBody WorkerCreateParam workerCreateParam,
                                     HttpServletRequest request) throws BusinessException {
        /*String authorization = request.getHeader("Authorization");
        log.info("网关透传的头:{}",authorization);
        LoginUser loginToken = SecurityContext.getLoginToken();
        log.info("passport-sdk做解析,得到LoginUser:{}",loginToken);*/
        //调用业务层 创建师傅数据
        Long workerId=workerService.create(workerCreateParam);
        return new Result<Long>(workerId);
        /**
         * {
         *     code:0,
         *     data:23
         * }
         */
    }
    /**
     * 师傅查询
     */
    @Autowired
    private WorkerAssemble workerAssemble;
    @GetMapping("/worker/detail")
    public Result<WorkerVO> detailWorker() throws BusinessException{
        //1. 业务层只能返回BO类型的数据
        WorkerBO workerBO=workerService.detail();
        //2. 转化成VO assemble 装配器,需要关注 装配对象是否是空,属性是否是空
        //在没有入驻申请时,登录的用户 查询的worker数据空的
        WorkerVO workerVO = workerAssemble.assembleBO2VO(workerBO);
        //3. 封装到Result返回
        return new Result<>(workerVO);
    }
}
